<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="approveStatus">
        <el-select v-model="queryParams.approveStatus" placeholder="审核状态" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.sys_user_appr_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="searchText">
        <el-input
          v-model="queryParams.searchText"
          placeholder="请输入关键词"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="apprList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="姓名" align="center" prop="userName" :show-overflow-tooltip="true"/>
      <el-table-column label="工号" align="center" prop="jobNumber" :show-overflow-tooltip="true"/>
      <el-table-column label="身份证号" align="center" prop="idcard" :show-overflow-tooltip="true"/>
      <el-table-column label="手机号" align="center" prop="phonenumber" :show-overflow-tooltip="true"/>
      <el-table-column label="所在部门" align="center" prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="注册时间" align="center" prop="createTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="approveTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="approveUserName"/>
      <el-table-column label="审核状态" align="center" prop="approveStatus" width="80">
        <template slot-scope="scope">
          {{ getApprStatus(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coordinate"
            @click="handleApprove(scope.row.userId)"
            v-hasPermi="['system:user:approve']"
            v-if="scope.row.approveStatus == 0"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row.userId)"
            v-hasPermi="['system:appr:apprList']"
            v-if="scope.row.approveStatus !== 0"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listUser} from "@/api/system/user";

export default {
  name: "Appr",
  dicts: ['sys_user_appr_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户审核流转记录表格数据
      apprList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apprResult: null,
        searchText: null,
        userType:'wechat'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户审核记录列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.apprList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getApprStatus(data) {
      if (data) {
        var obj= this.dict.type.sys_user_appr_status.find(d => d.value === String(data.approveStatus))
        if(obj){
          return obj.label
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        apprUserId: null,
        apprResult: null,
        apprOppinion: null,
        opreationType: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleApprove(data) {
      this.$router.push({path: "/userAppr", query: {id: data}});
    },
    handleView(data) {
      this.$router.push({path: "/userApprView", query: {id: data}});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户审核记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppr(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户审核记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppr(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppr(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户审核记录编号为"' + ids + '"的数据项？').then(function () {
        return delAppr(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appr/export', {
        ...this.queryParams
      }, `appr_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
